﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using sexhealth.console.service.post.vo;
using System.Collections;
using sexhealth.console.service.post;
using System.Text;
public partial class usercontrol_RightPostListDate : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request["chid"] != null)
            {

                int chid = int.Parse(Request["chid"]);
                if (chid == 10)
                {
                    pnlWhatis02.Visible = true;
                }
                else 
                {
                    pnlWhatis02.Visible = false;
                }
                PostService postService = new PostService();
                 //性福好站連結channelId=8,子分類20
                if (chid == 8 || chid == 20)
                {
                    IList<PostVO> postList = postService.GetPostList(20);
                    if (postList.Count > 0) { Response.Redirect("postdetail.aspx?chid=20&pid="+postList[0].PostNo); }
                }
                if (chid == 2 ) 
                {
                    IList<PostVO> postList = postService.GetPostList(2);
                    if (postList.Count > 0) { Response.Redirect("postdetail.aspx?chid=2&pid=" + postList[0].PostNo); }
                }
                if (chid == 3)
                {
                    IList<PostVO> postList = postService.GetPostList(3);
                    if (postList.Count > 0) { Response.Redirect("postdetail.aspx?chid=3&pid=" + postList[0].PostNo); }
                }
                ChannelVO ch = postService.DaoGetVOById<ChannelVO>(chid);
                StringBuilder sb = new StringBuilder();
                if (ch.ParentId == 0)
                {
                    IList<ChannelVO> childChannelList = postService.GetChannelList(chid);
                    if (childChannelList.Count > 0)
                    {
                        IList<PostVO> postList = postService.GetPostList(childChannelList[0].ChannelNo);
                        foreach (PostVO post in postList)
                        {
                            sb.Append("<tr>");
                            sb.Append("<td height='36' bgcolor='#E1E1E1'>");
                            sb.Append("<table width='100%' border='0' cellpadding='0' cellspacing='0'>");
                            sb.Append("<tr>");
                            sb.Append("<td width='4%' valign='top'>");
                            sb.Append("<img src='image/icon02.gif' width='7' height='6' class='icon-u' />");
                            sb.Append("</td>");
                            sb.Append("<td width='84%'>");
                            sb.Append("<a href='postdetail.aspx?chid=" + post.BelongToChannelId + "&pid=" + post.PostNo + "' class='b07'>" + post.Title + "</a>");
                            sb.Append("</td>");
                            sb.Append("<td width='12%' class='text02'>");
                            sb.Append(post.CreatedDate.Value.ToString("yyyy.MM.dd"));
                            sb.Append("</td>");
                            sb.Append("</tr>");
                            sb.Append("</table>");
                            sb.Append("</td>");
                            sb.Append("</tr>");
                        }
                        ltlPostList.Text = sb.ToString();
                        ltlSubChannelName.Text = childChannelList[0].Name;
                        ltlLocationName.Text = childChannelList[0].Name;
                        ltlSubChannelDescription.Text = childChannelList[0].DescriptionHtmlContent;
                    }
                }
                else
                {
                    IList<PostVO> postList = postService.GetPostList(chid);
                    foreach (PostVO post in postList)
                    {
                        sb.Append("<tr>");
                        sb.Append("<td height='36' bgcolor='#E1E1E1'>");
                        sb.Append("<table width='100%' border='0' cellpadding='0' cellspacing='0'>");
                        sb.Append("<tr>");
                        sb.Append("<td width='4%' valign='top'>");
                        sb.Append("<img src='image/icon02.gif' width='7' height='6' class='icon-u' />");
                        sb.Append("</td>");
                        sb.Append("<td width='84%'>");
                        sb.Append("<a href='postdetail.aspx?chid=" + post.BelongToChannelId + "&pid=" + post.PostNo + "' class='b07'>" + post.Title + "</a>");
                        sb.Append("</td>");
                        sb.Append("<td width='12%' class='text02'>");
                        sb.Append(post.CreatedDate.Value.ToString("yyyy.MM.dd"));
                        sb.Append("</td>");
                        sb.Append("</tr>");
                        sb.Append("</table>");
                        sb.Append("</td>");
                        sb.Append("</tr>");
                    }
                    ltlPostList.Text = sb.ToString();
                    ltlSubChannelName.Text = ch.Name;
                    ltlLocationName.Text = ch.Name;
                    ltlSubChannelDescription.Text = ch.DescriptionHtmlContent;
                }
            }
        }
    }

}
